route.ts

route.ts

基本信息

  • 类型: API 路由
  • 路径: ./src/app/api/prompts/[id]/vote/route.ts
  • 路由: /api/prompts/[id]/vote

概述

Prompt 投票(点赞)接口。支持为 Prompt 投票和取消投票。

HTTP 方法

  • POST: 为 Prompt 投票(点赞)
  • DELETE: 取消投票

路径参数

参数类型必填说明
idstringPrompt ID

POST - 投票(点赞)

响应

成功 (200)

{
  "voted": true,
  "voteCount": 15
}

错误

未授权 (401)
{
  "error": "unauthorized",
  "message": "You must be logged in"
}
未找到 (404)
{
  "error": "not_found",
  "message": "Prompt not found"
}
已投票 (400)
{
  "error": "already_voted",
  "message": "You have already upvoted this prompt"
}

DELETE - 取消投票

响应

成功 (200)

{
  "voted": false,
  "voteCount": 14
}

错误

未授权 (401)
{
  "error": "unauthorized",
  "message": "You must be logged in"
}
服务器错误 (500)
{
  "error": "server_error",
  "message": "Something went wrong"
}

依赖

  • next/server - Next.js 服务器组件
  • @/lib/auth - 认证
  • @/lib/db - Prisma 数据库客户端

权限

  • 需要登录

数据模型

投票记录在数据库中使用复合唯一索引 userId_promptId,确保每个用户对每个 Prompt 只能投一次票。

← 返回目录